/**
 * 城市搜索
 * @author  Jello
 * @create  2018/10/23 14:50
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/

$(function() {

	//取得href中的eara_city参数
	var area_city = getParam('area');
	var searchText = decodeURI(area_city); //解码
	if(searchText =="" || searchText == null){
		$("#current_city").html("您还没有最近的访问城市");
	}else{
		$("#current_city").html(searchText);
	}
	
	mui.ready(function() {
		var header = document.querySelector('header.mui-bar');
		var list = document.getElementById('list');
		//calc hieght
		list.style.height = (document.body.offsetHeight - header.offsetHeight) + 'px';
		//create
		window.indexedList = new mui.IndexedList(list);
	});

	init();

	//热门城市
	$(".mui-content .mui-indexed-list .mui-indexed-list-inner .mui-recently2").on("click", ".rece-btn", function() {
		var id_num = $(this).text();
		saveHistory(id_num);
		var searchUrl2 = encodeURI("recruitmentlist.html?area=" + id_num);
		window.location.href = searchUrl2;
	})

	//最近访问-全国
	$(".mui-content .mui-indexed-list .mui-indexed-list-inner .mui-recently").on("click", ".nationwide", function() {
		var id_num = $(this).text();
		var searchUrl2 = encodeURI("recruitmentlist.html?area=" + id_num);
		window.location.href = searchUrl2;
	})

	//城市
	$(".mui-content .mui-indexed-list .mui-indexed-list-inner .mui-table-view").on("click", ".mui-table-view-cell", function() {
		var id_num = $(this).text();
		saveHistory(id_num);
		var searchUrl2 = encodeURI("recruitmentlist.html?area=" + id_num);
		window.location.href = searchUrl2;
	})

	//初始化
	function init() {
		initHistory();
	}

	//最近访问
	function bindHistoryClick() {
		$('.mui-content .mui-indexed-list .mui-indexed-list-inner .r_visited').on('click', '.rece-btn', function() {
			var id_num = $(this).text();
			saveHistory(id_num);
			var searchUrl2 = encodeURI("recruitmentlist.html?area=" + id_num);
			window.location.href = searchUrl2;
		});

	}

	//取数据
	function saveHistory(id_num) {
		var hotelHistory = localStorage.getItem('hotelHistory');
		if(!hotelHistory) {
			hotelHistory = '';
		}
		if(hotelHistory.indexOf(id_num) > -1) {
			var arr = hotelHistory.split(',');
			var newArr = [];
			$.each(arr, function(i, item) {
				if(item !== id_num) {
					newArr.push(item);
				}
			})
			hotelHistory = newArr.join(',');
		}
		localStorage.setItem('hotelHistory', id_num + ',' + hotelHistory);
		initHistory();
	}

	function initHistory() { // 历史记录渲染
		var hotelHistory = localStorage.getItem('hotelHistory');
		//				if(hotelHistory == "" || hotelHistory == null) {
		//					$(".r_null").css("display", "block");
		//				}
		var history = '';
		if(hotelHistory) {
			$.each(hotelHistory.split(','), function(i, item) {
				if(item) {
					history += '<div class="rece-btn">' + item + '</div>'
				}
			})
		}
		$('.r_visited').html(history);
		bindHistoryClick();
	}

	//			function clearHistory() {
	//				localStorage.setItem('hotelHistory', '');
	//				initHistory();
	//			}

});